﻿/*
* ---------------------------------------------------
* 作者: MTQ 
* 日期： 2018-09-05 15:18:32 
* ---------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using MTQ.Model;
using MTQ.BLL;
using MTQ.IBLL;
using MTQ.Common;

namespace MTQ.Web.Controllers
{

    public partial class Base_RoleController : BaseController
    {

        private IBase_RoleService ibase_roleService = new Base_RoleService();


        //                                           
        // GET: //         
        [AccountAuth(Roles = "Base_Role_Index")]
        public ActionResult Index()
        {
            return View();
        }


        [AccountAuth(Roles = "Base_Role_Index")]
        public ActionResult Search()
        {

            Base_RoleQuery base_roleQuery = new Base_RoleQuery();
            //ServertBootStrapTable servertBootStrapTable = new ServertBootStrapTable();
            //servertBootStrapTable = ibase_roleService.LoadBootstrapSearchData(base_roleQuery, Request);
            List<LayuiTree> listLayuitree = new List<LayuiTree>();

            Base_RoleGroupService groupService = new Base_RoleGroupService();
            List<Base_RoleGroup> listGroup = groupService.LoadEntities(x => x.BG_Available == EnumAvailable.Enable).ToList();

            Base_RoleService roleService = new Base_RoleService();
            base_roleQuery.Role_Available = EnumAvailable.Enable;
            List<Base_Role> listRole = roleService.LoadSearchData(base_roleQuery).ToList();
            foreach (var group in listGroup)
            {
                LayuiTree layuitree = new LayuiTree() { id = group.BG_Id.ToString(), title = group.BG_Name };
                layuitree.children = new List<LayuiTree>();
                layuitree.spread = true;
                layuitree.forbiddel = true;
                layuitree.forbidupdate = true;
                layuitree.attrName = "O";

                foreach (var role in listRole.Where(x => x.Role_GroupId == group.BG_Id))
                {
                    LayuiTree children = new LayuiTree() { id = role.Role_Id.ToString(), title = role.Role_Name };
                    children.forbidadd = true;
                    children.attrName = "R";
                    layuitree.children.Add(children);
                }

                listLayuitree.Add(layuitree);
            }

            return Json(listLayuitree, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SearchAll()
        {
            Base_RoleService roleService = new Base_RoleService();
            Base_RoleQuery base_roleQuery = new Base_RoleQuery();
            base_roleQuery.Role_Available = EnumAvailable.Enable;
            List<Base_Role> listRole = roleService.LoadSearchData(base_roleQuery).ToList();
            return Json(listRole, JsonRequestBehavior.AllowGet);
        }


        //          
        // GET: /Base_Role/Create          
        [AccountAuth(Roles = "Base_Role_Create")]
        public ActionResult Create()
        {
            Base_Role base_role = new Base_Role();
            base_role.Role_Id = System.Guid.NewGuid();

            //Base_RoleGroupService groupService = new Base_RoleGroupService();
            //List<Base_RoleGroup> listGroup = groupService.LoadEntities(x => x.BG_Available == EnumAvailable.Enable).ToList();
            //ViewBag.listGroup = listGroup;

            return View(base_role);
        }

        //          
        // POST: /Base_Role/Create          
        [HttpPost]
        [AccountAuth(Roles = "Base_Role_Create")]
        public ActionResult Create(Base_Role base_role)
        {
            ModelMessage listMsg = null;
            if (ModelState.IsValid)
            {
                Guid guid = System.Guid.NewGuid();
                base_role.Role_Id = guid;
                base_role.Role_Available = EnumAvailable.Enable;

                var result = ibase_roleService.AddEntity(base_role);
                listMsg = new ModelMessage("ChangeResult", "m001", result != null ? "保存成功" : "保存失败", guid.ToString());
            }
            return Json(listMsg);
        }


        [AccountAuth(Roles = "Base_Role_Edit")]
        public ActionResult Edit(Guid? primarykey)
        {
            Base_Role base_role = ibase_roleService.LoadEntities(x => x.Role_Id == primarykey).FirstOrDefault();
            if (base_role == null)
            {
                return HttpNotFound();
            }
            return View(base_role);
        }

        [HttpPost]
        [AccountAuth(Roles = "Base_Role_Edit")]
        public ActionResult Edit(Base_Role base_role)
        {
            List<ModelMessage> listMsg = new List<ModelMessage>();
            if (ModelState.IsValid)
            {
                try
                {
                    bool result = ibase_roleService.UpdateEntity(base_role);
                    listMsg.Add(new ModelMessage("ChangeResult", "m001", result ? "保存成功" : "保存失败"));
                }
                catch (Exception e)
                {
                    listMsg.Add(new ModelMessage("ChangeResult", "m002", "保存失败"));
                }
            }
            return Json(listMsg);

        }

        [HttpPost]
        [AccountAuth(Roles = "Base_Role_Edit")]
        public ActionResult ActivateSwich(string primarykeys, string activate = "N")
        {
            ModelMessage modelMessage = new ModelMessage();

            if (OperatorAvailable(primarykeys, activate))
            {
                modelMessage.Code = "m001";
                modelMessage.Message = "修改成功";
            }
            else
            {
                modelMessage.Code = "m002";
            }
            return Json(modelMessage, JsonRequestBehavior.AllowGet);
        }

        private bool OperatorAvailable(string primarykeys, string available)
        {
            List<Guid> listBase_RoleGUID = primarykeys.Split(',').ToListGuid();
            List<Base_Role> listBase_Role = ibase_roleService.LoadEntities(x => listBase_RoleGUID.Contains(x.Role_Id)).ToList();
            foreach (var item in listBase_Role)
            {
                item.Role_Available = available;
            }
            int updaterow = ibase_roleService.UpdateEntity();
            if (updaterow == 0)
            {
                return false;
            }
            return true;
        }


        //          
        // POST: /Base_Role/Create          
        [HttpPost]
        [AccountAuth(Roles = "Base_Role_Create")]
        public ActionResult CreateRole(Base_Role base_role)
        {
            ModelMessage listMsg = new ModelMessage();
            listMsg = new ModelMessage("ChangeResult", "m001", "保存成功");
            //if (ModelState.IsValid)
            //{
            //    var result = ibase_roleService.AddEntity(base_role);
            //    listMsg =new ModelMessage("ChangeResult", "m001", result != null ? "保存成功" : "保存失败");
            //}
            return Json(listMsg);
        }


        [HttpGet]
        [AccountAuth(Roles = "Base_Role_View")]
        public ActionResult GetButtonAction(string primarykey)
        {

            Base_RoleButtonService rbs = new Base_RoleButtonService();

            System.Guid guid = new Guid(primarykey);
            var list = rbs.LoadEntities(x => x.Role_Id == guid).Select(x => x.Button_Id).ToList();

            return Json(list, JsonRequestBehavior.AllowGet);

        }
        public ActionResult SaveButtonAction()
        {
            ModelMessage listMsg = null;
           // List<string> primarykeys
           var primarykeys = Request.Form["primarykeys"];
            var roleid = Request.Form["roleid"];

            if (string.IsNullOrEmpty(roleid))
            {
                listMsg = new ModelMessage("ChangeResult", "m001", "保存失败");
                return Json(listMsg);
            }

            Base_ButtonService bs = new Base_ButtonService();

            bool result = bs.SaveAuth(new Guid(roleid), primarykeys);

             listMsg = new ModelMessage("ChangeResult", "m001", result  ? "保存成功" : "保存失败");
            return Json(listMsg);
        }


    }
}


